#include "idevice.h"

#include <sstream>

#include "json_util.h"


IDevice::IDevice(uint16_t bay_id, uint16_t dev_id, IPhyLink *phy_dev) :
    enabled_(true),
    onlinestate_(false),
    Regstatus(false),
    bay_id_(bay_id),
    dev_id_(dev_id),
    phy_dev_(phy_dev),
    phylink_mgr_(nullptr)
{

}

IDevice::~IDevice()
{

}

const std::string IDevice::devName() const
{
    std::ostringstream ss;
    ss << bayId() << "." << devId();

    return ss.str();
}

bool IDevice::parseDevInfo(cJSON *model_root)
{
    cJSON_GetString(model_root, "ManuID", dev_info_.manuID);
    cJSON_GetString(model_root, "ManuName", dev_info_.manuName);
    cJSON_GetString(model_root, "DeviceType", dev_info_.deviceType);
    cJSON_GetString(model_root, "NodeID", dev_info_.nodeID);
    cJSON_GetString(model_root, "ProductID", dev_info_.productID);
    cJSON_GetString(model_root, "Desc", dev_info_.desc);

    if(dev_info_.manuID.empty())
    {
        dev_info_.manuID = "null";
    }

    return true;
}

void IDevice::getActions(Actions &ykinfos)
{
    //遥控
    ykinfos.clear();
    yk_infos_mtx_.lock();
    for (auto it = yk_infos_.begin(); it != yk_infos_.end(); ++it)
    {
        if (it->second->step == Action::REPORT)
        {
            ykinfos.push_back(*(it->second));
            it->second->timestamp = 0;
            it->second->step = Action::IDLE;
        }
    }
    yk_infos_mtx_.unlock();
}
